/**
 * 
 */
package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipCheck;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipCheckDao;
import cc.comac.mom.entity.EquipCheckEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.MomConstants;
import cc.comac.mom.util.ResultBean;

/**
 * @Description:
 * @author: fyt
 * @date: 2020年7月21日下午2:13:13
 */
@Component
@Transactional
public class EquipCheckDataHandler extends ServiceImpl<EquipCheckDao, EquipCheckEntity> {

	@Autowired
	private EquipCheckDao equipCheckDao;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	@Autowired
	private IdFactory idFactory;

	public ResultBean save(EquipCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if (obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipCheckEntity innerData = obj.getInnerData();
		if (innerData == null) {
			throw new RuntimeException("数据为空");
		}
		if (innerData.getCheckNumber() == null || "".equals(innerData.getCheckNumber())) {
			innerData.setCheckNumber(idFactory.generateEquipCheckNo());
		}
		innerData.setCheckStatus(MomConstants.EQUIP_CHECK_STATUS_UNCONFIRM);
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipCheckDao.insert(innerData);
		if (insert < 1) {
			throw new RuntimeException("新增失败");
		}
		resultBean.setMessage(String.valueOf(innerData.getId()));
		return resultBean;
	}

	public ResultBean update(EquipCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if (obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipCheckEntity innerData = obj.getInnerData();
		if (innerData == null) {
			throw new RuntimeException("数据为空");
		}
		if (innerData.getCheckNumber() == null || "".equals(innerData.getCheckNumber())) {
			innerData.setCheckNumber(idFactory.generateEquipCheckNo());
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int update = equipCheckDao.updateById(innerData);
		if (update < 1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean deleteBatch(List<EquipCheck> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		
		for(EquipCheck item:list) {
			EquipCheckEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");
			}
			listIds.add(innerData.getId());
		}
		List<EquipCheckEntity> selectBatchIdsList = equipCheckDao.selectBatchIds(listIds);
		if(selectBatchIdsList == null || selectBatchIdsList.size()<1) {
			throw new RuntimeException("数据为空");
		}
		for(EquipCheckEntity item :selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = updateBatchById(selectBatchIdsList);
		
		if(!updateBatchById) {
			throw new RuntimeException("删除失败");
		}

		return resultBean;
	}
	
	public ResultBean checkConfirm(List<Long> listId) {
		ResultBean resultBean = new ResultBean(true);
		if(listId == null || listId.size()<1) {
			throw new RuntimeException("数据为空");
		}
		List<EquipCheckEntity> selectBatchIds = equipCheckDao.selectBatchIds(listId);
		if(selectBatchIds == null || selectBatchIds.size()<1) {
			throw new RuntimeException("数据为空");
		}
		for(EquipCheckEntity item :selectBatchIds) {
			item.setCheckStatus(MomConstants.EQUIP_CHECK_STATUS_CONFIRM);
			baseEntityDataUtil.updateBaseEntityData(item);
		}
		boolean updateBatchById = super.updateBatchById(selectBatchIds);
		if(!updateBatchById) {
			throw new RuntimeException("盘点确认失败");
		}
		return resultBean;
	}
	
	public ResultDev selectByCondition(EquipCheck obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		QueryWrapper<EquipCheckEntity> queryWrapper = new QueryWrapper<EquipCheckEntity>();
		queryWrapper.eq("row_status", 1);

		if (obj.getInnerData().getCheckNumber() != null && !"".equals(obj.getInnerData().getCheckNumber())) {
			queryWrapper.eq("check_number", obj.getInnerData().getCheckNumber());
		}
		if (obj.getInnerData().getCreator() != null && !"".equals(obj.getInnerData().getCreator())) {
			queryWrapper.eq("creator", obj.getInnerData().getCreator());
		}
		queryWrapper.orderByDesc("creation_time");
		Page<EquipCheckEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipCheckEntity> iPage = equipCheckDao.selectPage(page, queryWrapper);
		List<EquipCheckEntity> list = iPage.getRecords();
		resultDev.setEntityList(list);
		resultDev.setPageCount((int)iPage.getPages());
		resultDev.setTotalQuantity((int)iPage.getTotal());
		return resultDev;
	}
	
	
}
